package controller;

import bean.Area;
import bean.ClassB;
import com.google.gson.Gson;
import service.IAreaService;
import service.IClassBService;
import service.Impl.AreaServiceImpl;
import service.Impl.ClassBServiceImpl;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ClassServlet",urlPatterns = "/class.do")
public class ClassServlet extends HttpServlet {

    IClassBService ics =new ClassBServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){
            listByPage(request,response);
        }else if(method.equals("addClass")){
            addClass(request,response);
        }else if(method.equals("updateClass")){
            updateClass(request,response);
        }
    }

    private void listByPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        String pageNoStr = request.getParameter("pageNo");
        String pageSizeStr =request.getParameter("pageSize");
        int pageNo =1;
        int pageSize =10;
        if(pageNoStr!=null&&pageSizeStr!=null){
            pageNo =Integer.parseInt(pageNoStr);
            pageSize =Integer.parseInt(pageSizeStr);
        }
        String f_className = request.getParameter("f_className");
        String f_areaName = request.getParameter("f_areaName");
        PageUtil<ClassB> classBPageUtil=null;
        if(f_className==null&&f_areaName==null){
            classBPageUtil = ics.listByPage(pageNo, pageSize);
        }else{
            classBPageUtil = ics.listByPage(pageNo, pageSize,f_className,f_areaName);

        }


        Gson gson = new Gson();
        String json = gson.toJson(classBPageUtil);
        System.out.println("json = " + json);
        PrintWriter out = response.getWriter();
        out.write(json);
    }
    private void addClass(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String pkidstr = request.getParameter("pkid");
        int pkid =0;
        if(pkidstr!=null){
            pkid =Integer.parseInt(pkidstr);
        }
        String f_className = request.getParameter("f_className");
        String f_leader = request.getParameter("f_leader");
        String f_tel = request.getParameter("f_tel");
        ClassB cb =new ClassB(pkid,f_className,f_leader,f_tel);
        System.out.println("cb = " + cb);
        boolean b = ics.addOneClass(cb);
        if(b){

            out.write("true");

        }else{
            out.write("false");
        }
    }
    private void updateClass(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String pkidstr = request.getParameter("pkid");
        int pkid =0;
        if(pkidstr!=null){
            pkid=Integer.parseInt(pkidstr);
        }
        if(pkid!=0){
            String f_leader = request.getParameter("f_leader");
            String f_tel = request.getParameter("f_tel");
            boolean b = ics.updateOneClass(pkid, f_leader, f_tel);
            if(b){
                out.write("true");
            }else{
                out.write("false");
            }
        }
    }

}
